import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const storeToSession = store => {
    // 当 store 初始化后调用
    store.subscribe((mutation, state) => {
      if (sessionStorage.getItem('store')){
          sessionStorage.removeItem('store')
      }
      sessionStorage.setItem('store', JSON.stringify(state))
    })
}

const store = new Vuex.Store({
    state: {
        token: '',
        login:false,
        current_stoke: {},
        globalDialog: false,
        all_data:{},
        cell_data:[],
        cell_group:[],
        pcs_data:[],
        msg: '',
        noDialog: {},

    },
    mutations: {
        
        set_msg(state, msg){
            state.msg = msg
        },

        login(state, o) {
            state.login = true
            state.token = o.token
        },
        logout(state)
        {
            state.login = false
        },

        noDialog(state)
        {
            if(state.current_stoke.cell !== undefined) {
                let key = JSON.stringify(state.current_stoke)
                state.noDialog[key] = false

                setTimeout(() => {
                    state.noDialog[key] = undefined
                }, 10 * 60 * 1000)
            }
            
        },

        cell_data(state, o)
        {
            state.cell_data = o
        },
        cell_group(state, o)
        {
            state.cell_group = o
        }
        ,
        pcs_data(state, o)
        {
            state.pcs_data = o
        },
        current(state, o) {
            state.current_stoke = o
            state.globalDialog = true
        },
        globalDialog(state, s) {
            state.globalDialog = s
        },
        allData(state, s) {
            state.all_data = s
        }
        
    },
    getters: {
        all_data(state) {
            return state.all_data
        },
        isLogin(state) {
            return state.login
        },
        token(state) {
            return state.token
        },

        pcs_data(state) {
            return state.pcs_data
        },
        cell_data(state) {
            return state.cell_data
        }
        ,
        cell_group(state) {
            return state.cell_group
        },

        msg(state) {

            return state.msg
        },
        
        current_stoke(state) {
            return state.current_stoke
        },
        globalDialog(state) {
            if(state.current_stoke.cell !== undefined) {
                let key = JSON.stringify(state.current_stoke)
                if(key in state.noDialog){
                    return false
                }
            }
            return state.globalDialog
        }
    },
    plugins: [storeToSession]
})

// store.replaceState(devState)
export default store